home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / mail / transpor / ifmail23.z / ifmail23 / ifmail / iflib / nodelock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-27  |  2.5 KB  |  159 lines

  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <signal.h>
  7. #include <errno.h>
  8. #include <sys/stat.h>
  9. #include "xutil.h"
  10. #include "lutil.h"
  11. #include "ftn.h"
  12.  
  13. #ifdef NEED_BSY
  14.  
  15. #ifdef DONT_HAVE_PID_T
  16. #define pid_t int
  17. #endif
  18.  
  19. extern char *bsyname(faddr *);
  20. extern void mkdirs(char*);
  21.  
  22. int nodelock(addr)
  23. faddr *addr;
  24. {
  25.     char *fn,*tfn,*p;
  26.     char tmp[16];
  27.     FILE *fp;
  28.     pid_t pid,mypid;
  29.  
  30.     debug(4,"try locking node %s",ascfnode(addr,0x1f));
  31.  
  32.     fn=bsyname(addr);
  33.     tfn=xstrcpy(fn);
  34.     if ((p=strrchr(tfn,'/'))) *++p='\0';
  35.     mypid=getpid();
  36.     sprintf(tmp,"aa%u",mypid);
  37.     tfn=xstrcat(tfn,tmp);
  38.     mkdirs(tfn);
  39.     if ((fp=fopen(tfn,"w")) == NULL)
  40.     {
  41.         logerr("$cannot open tmp file for bsy lock \"%s\"",tfn);
  42.         free(tfn);
  43.         return 1;
  44.     }
  45.     fprintf(fp,"%10u\n",mypid);
  46.     fclose(fp);
  47.     chmod(tfn,0444);
  48.     if (link(tfn,fn) == 0)
  49.     {
  50.         debug(4,"created lock OK");
  51.         unlink(tfn);
  52.         free(tfn);
  53.         return 0;
  54.     }
  55.     else
  56.     {
  57.         debug(4,".bsy file present, check staleness");
  58.     }
  59.  
  60.     if (errno != EEXIST)
  61.     {
  62.         logerr("$could not link \"%s\" to \"%s\"",tfn,fn);
  63.         unlink(tfn);
  64.         free(tfn);
  65.         return 1;
  66.     }
  67.  
  68.     if ((fp=fopen(fn,"r")) == NULL)
  69.     {
  70.         logerr("$could not open existing lock file \"%s\"",fn);
  71.         unlink(tfn);
  72.         free(tfn);
  73.         return 1;
  74.     }
  75.  
  76.     fscanf(fp,"%d",&pid);
  77.     fclose(fp);
  78.     debug(4,"opened bsy file for pid %d",pid);
  79.     if (kill(pid,0) && (errno == ESRCH))
  80.     {
  81.         loginf("found stale bsy file for %s, unlink",
  82.             ascfnode(addr,0x1f));
  83.         unlink(fn);
  84.     }
  85.     else
  86.     {
  87.         debug(4,"process active, lock failed");
  88.         unlink(tfn);
  89.         free(tfn);
  90.         return 1;
  91.     }
  92.  
  93.     if (link(tfn,fn) == 0)
  94.     {
  95.         debug(4,"created lock OK");
  96.         unlink(tfn);
  97.         free(tfn);
  98.         return 0;
  99.     }
  100.     else
  101.     {
  102.         logerr("$could not link \"%s\" to \"%s\"",tfn,fn);
  103.         unlink(tfn);
  104.         free(tfn);
  105.         return 1;
  106.     }
  107. }
  108.  
  109. int nodeulock(addr)
  110. faddr *addr;
  111. {
  112.     char *fn;
  113.     FILE *fp;
  114.     pid_t pid,mypid;
  115.  
  116.     debug(4,"try unlocking node %s",ascfnode(addr,0x1f));
  117.  
  118.     fn=bsyname(addr);
  119.     if ((fp=fopen(fn,"r")) == NULL)
  120.     {
  121.         logerr("$cannot open lock file \"%s\"",fn);
  122.         return 1;
  123.     }
  124.     fscanf(fp,"%u",&pid);
  125.     fclose(fp);
  126.     mypid=getpid();
  127.  
  128.     if (pid == mypid)
  129.     {
  130.         debug(4,"unlinking lock file");
  131.         unlink(fn);
  132.         return 0;
  133.     }
  134.     else
  135.     {
  136.         logerr("trying unlock file for process %u, we are %u",
  137.             pid,mypid);
  138.         return 1;
  139.     }
  140. }
  141.  
  142. #else /* don't NEED_BSY */
  143.  
  144. int nodelock(addr)
  145. faddr *addr;
  146. {
  147.     debug(4,"try locking node %s - no locking",ascfnode(addr,0x1f));
  148.     return 0;
  149. }
  150.  
  151. int nodeulock(addr)
  152. faddr *addr;
  153. {
  154.     debug(4,"try unlocking node %s - no locking",ascfnode(addr,0x1f));
  155.     return 0;
  156. }
  157.  
  158. #endif /* NEED_BSY */
  159.